Kwik.Page.include("Kwik.Drawing.Rectangle.js");
Kwik.Page.include("Kwik.Drawing.Point.js");
/** 
* Screen device object
* @singleton
* @extends Kwik.Object
* @requires Kwik.Object
* @requires Kwik.Drawing.Point
* @requires Kwik.Drawing.Rectangle
* @created 12:02 PM 7/16/2007
* @modified 10:29 AM 4/16/2008
*/
Kwik.Ui.Devices.Screen =
{
	scrollX:0,
	scrollY:0,
	w: 0,
	h: 0,
	trackers: {},
	trackerCount: 0,

	/** Add a mouse tracker to the receive status changes */
	register: function Screen_register(tracker)
	{
		if(Kwik.Ui.Devices.Screen.trackerCount == 0)
		{
			Kwik.Ui.Devices.Screen._attachEvents();
		}

		var id = "tracker" + tracker.ptr.toString();
		if(id in Kwik.Ui.Devices.Screen.trackers){return;}

		Kwik.Ui.Devices.Screen.trackers[id] = tracker;
		Kwik.Ui.Devices.Screen.trackerCount++;
	},

	/** Remove a mouse tracker */
	unregister: function Screen_unregister(tracker)
	{

		var id = "tracker" + tracker.ptr.toString();
		if(id in Kwik.Ui.Devices.Screen.trackers)
		{
			delete Kwik.Ui.Devices.Screen.trackers[id];
			Kwik.Ui.Devices.Screen.trackerCount--;
		}
		else
		{
			return;
		}
		if(Kwik.Ui.Devices.Screen.trackerCount == 0)
		{
			Kwik.Ui.Devices.Screen._detachEvents();
		}
	},

	_attachEvents: function()
	{
		Kwik.Page.log.write("Kwik.Ui.Devices.Screen._attachEvents: Attaching events.");

		Kwik.Utility.attachEvent(window, "onresize", Kwik.Ui.Devices.Screen._windowResize);
		Kwik.Utility.attachEvent(window, "onscroll", Kwik.Ui.Devices.Screen._windowScroll);
	},

	_detachEvents: function()
	{
		Kwik.Page.log.write("Kwik.Ui.Devices.Screen._detachEvents: Detaching events.");

		Kwik.Utility.detachEvent(document.body, "onresize", Kwik.Ui.Devices.Screen._windowResize);
		Kwik.Utility.detachEvent(window, "onscroll", Kwik.Ui.Devices.Screen._windowScroll);
	},

	_windowResize: function Screen__windowResize(e)
	{
		Kwik.Page.log.write("Kwik.Ui.Devices.Screen._windowResize: ");
		var current = {w:this.getTotalWidth(), h: this.getTotalHeight()};
		if(Kwik.Ui.Devices.Screen.w === current.w && Kwik.Ui.Devices.Screen.h === current.h){return;}
		for(e in Kwik.Ui.Devices.Screen.trackers)
		{
			Kwik.Ui.Devices.Screen.trackers[e].resize(current.w, current.h);
		}
	},

	_windowScroll: function(e)
	{
		Kwik.Page.log.write("Kwik.Ui.Devices.Screen._windowScroll: ");

		var current = {x:this.getScrollLeft(), y: this.getScrollTop()};
		if(Kwik.Ui.Devices.Screen.scrollX === current.x && Kwik.Ui.Devices.Screen.scrollY === current.y){return;}
		for(e in Kwik.Ui.Devices.Screen.trackers)
		{
			Kwik.Ui.Devices.Screen.trackers[e].scroll(current.w, current.h);
		}
	},

	/** Amount of pixels scrolled down @type Number*/
	getScrollTop: function()
	{
		if(document.documentElement.scrollTop){return document.documentElement.scrollTop;}
		if(window.pageYOffset){return window.pageYOffset;}
		return 0;
	},
	/** Amount of pixels scrolled right @type Number*/
	getScrollLeft: function()
	{
		if(document.documentElement.scrollLeft){return document.documentElement.scrollLeft;}
		if(window.pageXOffset){return window.pageXOffset;}
		return 0;
	},
	/** Get the visible width in pixels @type Number */
	getVisibleWidth: function()
	{
		if(Kwik.Ui.Devices.Screen.getScrollLeft() == 0){return Kwik.Ui.Devices.Screen.getTotalWidth();}
		return document.documentElement.clientWidth;
	},
	/** Get the visible height in pixels @type Number */
	getVisibleHeight: function()
	{
		if(Kwik.Ui.Devices.Screen.getScrollTop() == 0){return Kwik.Ui.Devices.Screen.getTotalHeight();}
		return document.documentElement.clientHeight;
	},
	_getBodyWidth: function()
	{
		return document.body.clientWidth;
	},
	_getBodyHeight: function()
	{
		return document.body.clientHeight;
	},
	_getDocumentWidth: function()
	{
		return document.documentElement.clientWidth;
	},
	_getDocumentHeight: function()
	{
		return document.documentElement.clientHeight;
	},
	/** Get the total width in pixels @type Number */
	getTotalWidth: function()
	{
		var bw = this._getBodyWidth();
		var dw = this._getDocumentWidth();
		return bw>dw? bw: dw;
	},
	/** Get the total width in pixels @type Number */
	getTotalHeight: function()
	{
		var bh = this._getBodyHeight();
		var dh = this._getDocumentHeight();
		return bh>dh? bh: dh;
	},
	/** Get the center of the total page size @type Kwik.Drawing.Point */
	getTotalCenter: function()
	{
		var rect = Kwik.Ui.Devices.Screen.getTotalRectangle();
		return rect.getCenter();
	},
	/** Get the center of the visible page size @type Kwik.Drawing.Point */
	getVisibleCenter: function()
	{
		var rect = Kwik.Ui.Devices.Screen.getVisibleRectangle();
		return rect.getCenter();
	},
	/** Get a rectangle of the total page @type Kwik.Drawing.Rectangle */
	getTotalRectangle: function()
	{
		return new Kwik.Drawing.Rectangle({
			x: 0,
			y: 0, 
			w: Kwik.Ui.Devices.Screen.getTotalWidth(),
			h: Kwik.Ui.Devices.Screen.getTotalHeight()
		});
	},
	/** Get a rectangle of the visible page @type Kwik.Drawing.Rectangle */
	getVisibleRectangle: function()
	{
		return new Kwik.Drawing.Rectangle({
			x: Kwik.Ui.Devices.Screen.getScrollLeft(),
			y: Kwik.Ui.Devices.Screen.getScrollTop(),
			w: Kwik.Ui.Devices.Screen.getVisibleWidth(),
			h: Kwik.Ui.Devices.Screen.getVisibleHeight()
		});
	}
};

Kwik.Ui.Devices.ScreenTracker = function(config)
{
	/** @ignore */
	Kwik.Utility.extend(this, "Kwik.Object");
	this.__class.push("Kwik.Ui.Devices.ScreenTracker");

	this.scrollX = 0;
	this.scrollY = 0;
	this.w = 0;
	this.h = 0;
	this.isTracking = false;

	this.start = function ScreenTracker_start()
	{
		this.fireEvent("onbeforestart");

		Kwik.Ui.Devices.Screen.register(this);
		this.isTracking = true;

		this.fireEvent("onstart");

	};

	this.stop = function ScreenTracker_stop()
	{
		if(this.isTracking === false){return;}
		this.fireEvent("onbeforestop");

		Kwik.Ui.Devices.Screen.unregister(this);
		this.isTracking = false;

		this.fireEvent("onstop");

	};

	this.resize = function ScreenTracker_resize(w, h)
	{
		if(w === this.w && h === this.h){return;}
		this.fireEvent("onbeforeresize", [this.w, this.h, w, h]);

		this.w = w;
		this.h = h;
		
		this.fireEvent("onresize", [this.w, this.h]);
	};

	this.scroll = function ScreenTracker_scroll(x, y)
	{
		if(x === this.scrollX && y === this.scrollY){return;}
		this.fireEvent("onbeforerescroll", [this.scrollX, this.scrollY, x, y]);

		this.scrollX = x;
		this.scrollY = y;
		
		this.fireEvent("onscroll", [this.scrollX, this.scrollY]);	
	};

	this.onbeforestart = function(){};
	this.onstart = function(){};

	this.onbeforestop = function(){};
	this.onstop = function(){};

	this.onbeforeresize = function(oldW, oldH, newW, newH){};
	this.onresize = function(w, h){};

	this.onbeforescroll = function(oldX, oldY, newX, newY){};
	this.onscroll = function(x, y){};

	/** @ignore */
	this.main = function ScreenTracker_main(config)
	{
		if(config){this.configure(config);}
	}; this.main(config);
};